////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __WN_FILE_H__
#define __WN_FILE_H__

#include "WNCore/inc/WNCore.h"

namespace WNFileIO {
    class WNFile {
    public:
        WNFile();
        ~WNFile();

        enum WNFileMode {
            WN_NONE         = 0,
            WN_READ         = 1,
            WN_WRITE        = (1 << 1),
            WN_EXCLUSIVE    = (1 << 2),
            WN_CREATE       = (1 << 3),
            WN_CLOBBER      = (1 << 4)
        };

        enum WNFileError {
            WN_FILE_OK,
            WN_FILE_NON_EXISTANT,
            WN_FILE_BAD_MODE,
            WN_FILE_TOO_LARGE,
            WN_FILE_BAD_LOCK,
            WN_FILE_ERROR
        };

        WNFileError     OpenFile(const WN_CHAR* _name, WN_UINT32 _mode );

        WN_CHAR*        getDataBuffer();

        WN_BOOL         CommitFileBuffer();
        WN_CHAR*        ResizeDataBuffer(WN_SIZE_T _size );

        static WN_BOOL  DoesFileExist(const WN_CHAR* _name );

    private:
        WN_UINT32       mFileMode;
        WN_SIZE_T       mMaxBufferSize;
        WN_SIZE_T       mFileSize;
        WN_CHAR*        mFileBuffer;
        WN_BOOL         mIsFileOpen;

        #ifdef _WN_WINDOWS
            HANDLE      mFileHandle;
        #elif defined _WN_LINUX
            FILE*       mFileHandle;
        #endif
    };
}

#endif // __WN_FILE_H__
